package com.econ.powercloud.ems.service.biz;

import com.alibaba.fastjson.JSON;
import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.exception.CommonRuntimeException;
import com.econ.powercloud.ems.common.enums.*;
import com.econ.powercloud.ems.common.util.IdWorker;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.dal.entity.*;
import com.econ.powercloud.ems.dal.entity.ext.IotViceProductYcExt;
import com.econ.powercloud.ems.domain.IotProductSyncDO;
import com.econ.powercloud.ems.pojo.EmsClusterConfigVO;
import com.econ.powercloud.ems.pojo.scene.*;
import com.econ.powercloud.ems.service.dao.*;
import com.econ.powercloud.ems.service.biz.scene.ProductDataBizService;
import com.econ.powercloud.ems.service.biz.scene.SceneBizService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : 同步iot2.0中关于ems相关的数据信息service
 * @Date : 2021/11/16 9:24
 * @Modified by : zjf
 */
@Service
public class SynchronousSceneDataService {
    @Autowired
    private SceneBizService sceneBizService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private EmsEnergyEquipmentDAO emsEnergyEquipmentDAO;
    @Autowired
    private EmsDeviceDAO emsDeviceDAO;
    @Autowired
    private EmsDeviceCalConfigDAO emsDeviceCalConfigDAO;
    @Autowired
    private EmsSyncIotProductDAO emsSyncIotProductDAO;
    @Autowired
    private ExtIotViceProductYcDAO extIotViceProductYcDAO;
    @Autowired
    private EmsProductLocationConfigDAO emsProductLocationConfigDAO;
    @Autowired
    private EmsComputeTagConfigDAO emsComputeTagConfigDAO;
    @Autowired
    private EmsClusterConfigDAO emsClusterConfigDAO;
    @Autowired
    private EmsColumnConfigDAO emsColumnConfigDAO;
    @Autowired
    private ProductDataBizService productDataBizService;

    /**
     * 根据EMS的储能站id与场景服务的场景编号 生成ems_device表中的数据
     * @param stationId:储能站id
     * @param sceneCode:场景编号
     * @param insertDeviceData : ems_device表中的新增信息
     * @param insertEquipmentData : 储能装置新增数据
     */
    public void initEmsDeviceData(String stationId,String sceneCode,List<EmsDevice> insertDeviceData,
                                  List<EmsEnergyEquipment> insertEquipmentData) {
        //调用场景服务的接口,获取该场景的结构图信息
        List<OpenApiSceneStructureVO> sceneStructure = sceneBizService.getSceneStructure(sceneCode, SysConstant.EMS_APPLICATION);
        if (CollectionUtils.isEmpty(sceneStructure)) {
            throw new CommonRuntimeException(StatusType.FAIL, "查询场景服务的结构图失败！");
        }
        OpenApiSceneStructureVO structure = sceneStructure.get(0);
        //key:nodeId,value:equipmentId
        Map<String,String> nodeIdToEquipmentId = Maps.newHashMap();
        this.dealWithOpenApiSceneStructureVO(structure,insertDeviceData,insertEquipmentData,nodeIdToEquipmentId,stationId);
        if(CollectionUtils.isNotEmpty(insertDeviceData)){
            emsDeviceDAO.deleteByStationId(stationId);
            emsDeviceDAO.saveBatch(insertDeviceData);
        }
        if(CollectionUtils.isNotEmpty(insertEquipmentData)){
            emsEnergyEquipmentDAO.deleteByStationId(stationId);
            emsEnergyEquipmentDAO.saveBatch(insertEquipmentData);
        }
    }

    public void updateEmsDeviceData(String stationId,String sceneCode,List<EmsDevice> insertDeviceData,
                                    List<EmsEnergyEquipment> insertEquipmentData){
        //调用场景服务的接口,获取该场景的结构图信息
        List<OpenApiSceneStructureVO> sceneStructure = sceneBizService.getSceneStructure(sceneCode, SysConstant.EMS_APPLICATION);
        if (CollectionUtils.isEmpty(sceneStructure)) {
            throw new CommonRuntimeException(StatusType.FAIL, "查询场景服务的结构图失败！");
        }
        List<EmsDevice> emsDeviceList = emsDeviceDAO.list();
        OpenApiSceneStructureVO structure = sceneStructure.get(0);
        //key:nodeId,value:equipmentId,初始化那些储能装置的配置信息
        Map<String,String> nodeIdToEquipmentId = emsDeviceList.stream().filter(d-> DeviceType.EQUIPMENT.getCode().equals(d.getDeviceType()))
                .collect(Collectors.toMap(EmsDevice::getDeviceCode,EmsDevice::getEnergyEquipmentId,(p1,p2)->p1));
        this.dealWithOpenApiSceneStructureVO(structure,insertDeviceData,insertEquipmentData,nodeIdToEquipmentId,stationId);
        //同步电池簇匹配位置
        Map<String,String> deviceCodeToSeatMap = emsDeviceList.stream().filter(d-> d.getSeat() != null)
                .collect(Collectors.toMap(EmsDevice::getDeviceCode,EmsDevice::getSeat,(p1,p2)->p1));
        if(CollectionUtils.isNotEmpty(insertDeviceData)){
            emsDeviceDAO.deleteByStationId(stationId);
            insertDeviceData.stream().forEach(d-> d.setSeat(deviceCodeToSeatMap.get(d.getDeviceCode())));
            emsDeviceDAO.saveBatch(insertDeviceData);
        }
        if(CollectionUtils.isNotEmpty(insertEquipmentData)){
            emsEnergyEquipmentDAO.deleteByStationId(stationId);
            emsEnergyEquipmentDAO.saveBatch(insertEquipmentData);
        }
    }

    private void dealWithOpenApiSceneStructureVO(OpenApiSceneStructureVO structure, List<EmsDevice> insertDeviceData,
                                                 List<EmsEnergyEquipment> insertEquipmentData, Map<String,String> nodeIdToEquipmentIdMap,
                                                 String stationId){
        EmsDevice emsDevice = EmsDevice.builder()
                .deviceCode(structure.getNodeId()).deviceName(structure.getNodeName())
                .productKey(structure.getProductKey()).moduleCode(structure.getModuleCode())
                .initDeviceCode(structure.getDeviceCode()).deviceType(DeviceType.fromCodeInScene(structure.getDeviceTypeCode()))
                .energyStationId(stationId).parentDeviceCode(structure.getParentNodeId())
                .gmtCreate(System.currentTimeMillis()).gmtModify(System.currentTimeMillis())
                .build();
        if(DeviceType.EQUIPMENT.getCodeInScene().equals(structure.getDeviceTypeCode())){
            //这里在做一层判断,如果是更新的操作,nodeIdToEquipmentIdMap中就会存在该数据(初始会赋值)，直接获取替代重新生成
            String equipmentId = nodeIdToEquipmentIdMap.get(structure.getNodeId()) == null?
                    idWorker.nextId()+"": nodeIdToEquipmentIdMap.get(structure.getNodeId());
            EmsEnergyEquipment emsEnergyEquipment = EmsEnergyEquipment.builder()
                    .energyEquipmentId(equipmentId).energyEquipmentName(structure.getNodeName())
                    .energyStationId(stationId).build();
            insertEquipmentData.add(emsEnergyEquipment);
            emsDevice.setEnergyEquipmentId(equipmentId);
            nodeIdToEquipmentIdMap.put(structure.getNodeId(),equipmentId);
        }else if(DeviceType.STATION.getCodeInScene().equals(structure.getDeviceTypeCode())){
            nodeIdToEquipmentIdMap.put(structure.getNodeId(),null);
        }else{
            String equipmentId = nodeIdToEquipmentIdMap.get(structure.getParentNodeId());
            emsDevice.setEnergyEquipmentId(equipmentId);
            nodeIdToEquipmentIdMap.put(structure.getNodeId(),equipmentId);
        }
        insertDeviceData.add(emsDevice);
        if(CollectionUtils.isEmpty(structure.getChildData())){
            return;
        }
        structure.getChildData().stream().forEach(d->{
            this.dealWithOpenApiSceneStructureVO(d,insertDeviceData,insertEquipmentData,nodeIdToEquipmentIdMap,stationId);
        });
    }

    /**
     * 同步场景服务中计算数据
     * @param emsDevices : 储能站下的设备集合
     * @param sceneCode : 场景编号
     */
    @Transactional(rollbackFor = Exception.class)
    public void initEmsDeviceCalConfig(List<EmsDevice> emsDevices, String sceneCode){
        //emsDevices按照initDeviceCode + moduleCode分组，目的场景中的设备与iot中的设备做一个匹配关系
        Map<String, EmsDevice> emsDeviceMap = emsDevices.stream().collect(
                Collectors.toMap(it -> it.getInitDeviceCode() + "." + it.getModuleCode(), d->d,(p1, p2)->p1));
        //获取该场景编号下的所有的计算点号
        List<OpenApiSceneCalculationVO> calculationVOS = sceneBizService.getSceneCalculation(sceneCode, SysConstant.EMS_APPLICATION);
        if(CollectionUtils.isEmpty(calculationVOS)){
            return;
        }
        List<EmsComputeTagConfig> emsComputeTagConfigList = emsComputeTagConfigDAO.list();
        Map<String,List<EmsComputeTagConfig>> tagConfigMap = emsComputeTagConfigList.stream().collect(Collectors.groupingBy(d->d.getComputeTag()));
        List<EmsDeviceCalConfig> insertList = Lists.newArrayList();
        for (OpenApiSceneCalculationVO calculationVO : calculationVOS){
            //如果这个计算点号没有标签map就不管
            if(MapUtils.isEmpty(calculationVO.getIdentificationMap())){
                continue;
            }
            //从标签map中查询对应标识
            if(calculationVO.getIdentificationMap().get(SceneIdentificationType.COMPUTE_DATA_LABEL.getCode()) == null){
                continue;
            }
            List<String> computeTags = Lists.newArrayList(calculationVO.getIdentificationMap().get(SceneIdentificationType.COMPUTE_DATA_LABEL.getCode()).split(","));
            for (String computeTag : computeTags){
                //判断对应的标识的 code是不是EMS中需要的计算点号标识
                if(CollectionUtils.isEmpty(tagConfigMap.get(computeTag))){
                    continue;
                }
                EmsDevice emsDevice = emsDeviceMap.get(calculationVO.getDeviceCode() + "." + calculationVO.getModuleCode());
                if(emsDevice == null){
                    continue;
                }
                tagConfigMap.get(computeTag).stream().forEach(d->{
                    EmsDeviceCalConfig emsDeviceCalConfig = EmsDeviceCalConfig.builder()
                            .computeCode(calculationVO.getComputeCode())
                            .deviceCode(emsDevice.getDeviceCode())
                            .location(d.getLocation())
                            .measureType(MeasureType.fromCodeInIot(calculationVO.getMeasureType()))
                            .name(d.getName())
                            .sorting(d.getSorting())
                            .complex(d.getComplex())
                            .virtual(VirtualType.VIRTUAL.getCode())
                            .build();
                    insertList.add(emsDeviceCalConfig);
                });
            }
        }
        if(CollectionUtils.isNotEmpty(insertList)){
            emsDeviceCalConfigDAO.deleteByDeviceCodes(emsDevices.stream().map(EmsDevice::getDeviceCode).collect(Collectors.toList()));
            emsDeviceCalConfigDAO.deleteByComputeCodes(insertList.stream().map(EmsDeviceCalConfig::getComputeCode).collect(Collectors.toList()));
            emsDeviceCalConfigDAO.saveBatch(insertList);
        }
    }

    /**
     * 初始化簇的电池pack配置
     * @param deviceList : 一个场景下的所有设备信息
     * @param clusterConfigVOS : 每个簇的模型配置信息,VO中的code为场景中的节点id,即emsdevice中的deviceCode
     */
    @Transactional(rollbackFor = Exception.class)
    public void initClusterConfigWithPack(List<EmsDevice> deviceList, List<EmsClusterConfigVO> clusterConfigVOS){
        //在这些设备中找到设备类型为簇的设备
        List<EmsDevice> clusterList = deviceList.stream().filter(d-> DeviceType.CLUSTER.getCode().equals(d.getDeviceType())).collect(Collectors.toList());
        //key: initDeviceCode + . + moduleCode
        Map<String, EmsDevice> deviceMap = clusterList.stream().collect(Collectors.toMap(d->d.getDeviceCode(), d->d,(p1, p2)->p1));
        List<EmsClusterConfig> insertList = Lists.newArrayList();
        for (EmsClusterConfigVO configVO : clusterConfigVOS){
            EmsDevice emsDevice = deviceMap.get(configVO.getDeviceCode());
            if(emsDevice == null){
                throw new CommonRuntimeException(StatusType.FAIL,"场景中未找到配置的电池簇模型中的电池簇！");
            }
            insertList.add(EmsClusterConfig.builder().deviceCode(emsDevice.getDeviceCode()).type(configVO.getType()).build());
        }
        if(CollectionUtils.isNotEmpty(insertList)){
            emsClusterConfigDAO.deleteByDeviceCodes(clusterList.stream().map(EmsDevice::getDeviceCode).collect(Collectors.toList()));
            emsClusterConfigDAO.saveBatch(insertList);
        }
    }

    /**
     * 配置一个储能站下的表报配置信息
     * @param deviceList : 某个储能站下的所有设备列表
     * @param equipmentList : 该储能站的储能装置配置信息
     * 配置电表的列数据  由于收益报表与电费确认结算表的一些原因导致电表相关的产品的配置是一个固定值
     * 配置BMU报表的列数据配置  目前的逻辑是一个储能装置下BMU的列的点号后缀都是一样的，不必考虑不同的情况
     */
    @Transactional(rollbackFor = Exception.class)
    public void initEmsColumnConfigInfo(List<EmsDevice> deviceList, List<EmsEnergyEquipment> equipmentList,Integer stationType){
        List<EmsColumnConfig> columnConfigList = Lists.newArrayList();
        //配置电表的列数据
        String stationId = equipmentList.get(0).getEnergyStationId();
        List<String> equipmentIds = equipmentList.stream().map(EmsEnergyEquipment::getEnergyEquipmentId).collect(Collectors.toList());
        for (MeterColumnConfigType configType : MeterColumnConfigType.values()){
            columnConfigList.add(EmsColumnConfig.builder()
                    .energyStationId(stationId).reportId(ReportIdType.METER.getCode()).deviceType(DeviceType.MULTIFUNCTION_METER.getCode())
                    .columnId(configType.getCode()).columnName(configType.getDesc()).sorting(configType.getSorting())
                    .build());
            equipmentIds.stream().forEach(d->{
                columnConfigList.add(EmsColumnConfig.builder()
                        .energyStationId(stationId).energyEquipmentId(d).reportId(ReportIdType.METER.getCode()).deviceType(DeviceType.MEASURING_METER.getCode())
                        .columnId(configType.getCode()).columnName(configType.getDesc()).sorting(configType.getSorting())
                        .build());
                if(StationType.HIGH_PRESSURE_CASCADE.getCode().equals(stationType)){
                    columnConfigList.add(EmsColumnConfig.builder()
                            .energyStationId(stationId).energyEquipmentId(d).reportId(ReportIdType.METER.getCode()).deviceType(DeviceType.DEVICE_METER.getCode())
                            .columnId(configType.getCode()).columnName(configType.getDesc()).sorting(configType.getSorting())
                            .build());
                }
            });
        }
        //配置BMU报表的列数据 配置查询BMU报表的tag标签有哪些
        List<SceneSpecialTagType> bmuTagTypeList = SceneSpecialTagType.getBMUReportTag();
        Map<String, SceneSpecialTagType> tagTypeMap = bmuTagTypeList.stream().collect(Collectors.toMap(d->d.getCode(), d->d,(p1, p2)->p1));
        Map<String,List<EmsDevice>> clusterMapByEquipment = deviceList.stream().filter(d-> DeviceType.CLUSTER.getCode().equals(d.getDeviceType()))
                .collect(Collectors.groupingBy(d->d.getEnergyEquipmentId()));
        //这里只选一个储能装置下的一个簇设备,组成一个List
        List<EmsDevice> searchEmsDevice = Lists.newArrayList();
        clusterMapByEquipment.forEach((k,v)->{
            searchEmsDevice.add(v.get(0));
        });
        //查询标签在iot_product_yc与其副表中的数据有哪些
        List<IotViceProductYcExt> ycExtList = extIotViceProductYcDAO.getBaseMapper().selectYcExtByProductKeysAndLabels(
                searchEmsDevice.stream().map(EmsDevice::getProductKey).collect(Collectors.toList()), Lists.newArrayList(tagTypeMap.keySet()));
        //key: productKey + . + moduleCode + . + label
        Map<String,IotViceProductYcExt> iotProductYcSyncMap = ycExtList.stream().collect(
                Collectors.toMap(d->d.getProductKey() + SysConstant.SPOT + d.getModuleCode() + SysConstant.SPOT + d.getVal5(), d->d,(p1, p2)->p1));
        bmuTagTypeList.stream().forEach(tagType->{
            searchEmsDevice.stream().forEach(device ->{
                IotViceProductYcExt ycExt = iotProductYcSyncMap.get(device.getProductKey() + SysConstant.SPOT + device.getModuleCode() + SysConstant.SPOT + tagType.getCode());
                if(ycExt == null){
                    throw new CommonRuntimeException(StatusType.FAIL,"BMU报表的列信息未在场景中配置！");
                }
                columnConfigList.add(EmsColumnConfig.builder()
                        .energyStationId(stationId).energyEquipmentId(device.getEnergyEquipmentId()).reportId(ReportIdType.BMU.getCode()).deviceType(DeviceType.CLUSTER.getCode())
                        .columnId(ycExt.getDit().replace(device.getModuleCode() + SysConstant.SPOT,"")).columnName(tagType.getDesc()).sorting(tagType.getSorting())
                        .build());
            });
        });
        if(CollectionUtils.isNotEmpty(columnConfigList)){
            emsColumnConfigDAO.deleteByStation(stationId);
            emsColumnConfigDAO.saveBatch(columnConfigList);
        }
    }

    /**
     * 同步场景中产品的yk/yt/yc/yx的所有数据
     * @param productKeys : 产品key集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void initProductYcYxInfo(List<String> productKeys){
        //同步产品的遥测配置
        this.initEmsYcConfig(productKeys);
        //同步产品的遥信配置
        this.initEmsYxConfig(productKeys);
    }

    /**
     * 同步场景中的产品与模块信息
     * @param productVOList : 场景中获取的产品的基本信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void initProductAndModuleInfo(List<OpenApiSceneProductVO> productVOList){
        List<String> productKeys = productVOList.stream().map(OpenApiSceneProductVO::getProductKey).collect(Collectors.toList());
        List<EmsSyncIotProduct> productSyncList = Lists.newArrayList();
        for (OpenApiSceneProductVO apiProductVO : productVOList){
            EmsSyncIotProduct emsSyncIotProduct = IotProductSyncDO.VOToEntity(apiProductVO);
            //赋值同步时间与修改时间
            emsSyncIotProduct.setSyncTime(System.currentTimeMillis());
            emsSyncIotProduct.setIotGmtModify(apiProductVO.getGmtModify());
            productSyncList.add(emsSyncIotProduct);
        }
        emsSyncIotProductDAO.deleteByProductKeys(productKeys);
        if(CollectionUtils.isNotEmpty(productSyncList)){
            emsSyncIotProductDAO.getBaseMapper().insertBatch(productSyncList);
        }
    }

    /**
     * 同步场景中的遥测点号(按照productKey来获取)
     * @param productKeys : 产品key集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void initEmsYcConfig(List<String> productKeys){
        List<EmsProductLocationConfig> locationConfigList = Lists.newArrayList();
        for (String productKey : productKeys){
            //从场景服务获取产品遥测点号配置信息
            List<OpenApiSceneProductYcVO> ycVOList = productDataBizService.getYcDataByProductKey(productKey);
            if(CollectionUtils.isEmpty(ycVOList)){
                continue;
            }
            for (OpenApiSceneProductYcVO ycVO : ycVOList){
                //如果这个遥测点号没有标签map就不管
                if(MapUtils.isEmpty(ycVO.getIdentificationMap())){
                    continue;
                }
                //如果这个遥测点号没有location标签就跳过
                String location = ycVO.getIdentificationMap().get(SceneIdentificationType.LOCATION.getCode());
                if(location == null){
                    continue;
                }
                //如果这个遥测点号没有orderNum标签跳过
                String orderNum = ycVO.getIdentificationMap().get(SceneIdentificationType.ORDER_NUM.getCode());
                if(orderNum == null){
                    continue;
                }
                //如果这个遥测点号没有name标签跳过
                String nameInEms = ycVO.getIdentificationMap().get(SceneIdentificationType.NAME_IN_EMS.getCode());
                if(nameInEms == null){
                    continue;
                }
                //组装ems_product_location_config配置表数据
                EmsProductLocationConfig emsProductLocationConfig = EmsProductLocationConfig.builder()
                        .productKey(productKey).moduleCode(ycVO.getModuleCode()).dit(ycVO.getDit())
                        .virtual(VirtualType.REAL.getCode()).location(Integer.parseInt(location))
                        .measureType(MeasureType.yc.getCode()).sorting(orderNum).name(nameInEms)
                        .unit(ycVO.getUnit())
                        .build();
                locationConfigList.add(emsProductLocationConfig);
                String otherLocation = ycVO.getIdentificationMap().get(SceneIdentificationType.OTHER_LOCATION.getCode());
                if(otherLocation == null){
                    continue;
                }
                //其他位置信息也放到ems_product_location_config配置表中
                List<OtherLocationVO> locationList = JSON.parseArray(otherLocation, OtherLocationVO.class);
                locationList.stream().forEach(d->{
                    EmsProductLocationConfig config = EmsProductLocationConfig.builder()
                            .productKey(productKey).moduleCode(ycVO.getModuleCode()).dit(ycVO.getDit())
                            .virtual(VirtualType.REAL.getCode()).location(d.getLocation())
                            .measureType(MeasureType.yc.getCode()).sorting(d.getOrderNum()).name(d.getName())
                            .unit(ycVO.getUnit())
                            .build();
                    locationConfigList.add(config);
                });
            }
        }
        //数据库删除与新增操作
        emsProductLocationConfigDAO.deleteByProductKeys(productKeys, MeasureType.yc.getCode());
        if(CollectionUtils.isNotEmpty(locationConfigList)){
            emsProductLocationConfigDAO.saveBatch(locationConfigList);
        }
    }

    /**
     * 同步场景中的遥信点号(按照productKey来获取)
     * @param productKeys : 产品key集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void initEmsYxConfig(List<String> productKeys){
        List<EmsProductLocationConfig> locationConfigList = Lists.newArrayList();
        for (String productKey : productKeys){
            //从场景服务获取产品遥测点号配置信息
            List<OpenApiSceneProductYxVO> yxVOList = productDataBizService.getYxDataByProductKey(productKey);
            if(CollectionUtils.isEmpty(yxVOList)){
                continue;
            }
            for (OpenApiSceneProductYxVO yxVO : yxVOList){
                //如果这个遥测点号没有标签map就不管
                if(MapUtils.isEmpty(yxVO.getIdentificationMap())){
                    continue;
                }
                //如果这个遥测点号没有location标签就跳过
                String location = yxVO.getIdentificationMap().get(SceneIdentificationType.LOCATION.getCode());
                if(location == null){
                    continue;
                }
                //如果这个遥测点号没有orderNum标签跳过
                String orderNum = yxVO.getIdentificationMap().get(SceneIdentificationType.ORDER_NUM.getCode());
                if(orderNum == null){
                    continue;
                }
                //如果这个遥测点号没有name标签跳过
                String nameInEms = yxVO.getIdentificationMap().get(SceneIdentificationType.NAME_IN_EMS.getCode());
                if(nameInEms == null){
                    continue;
                }
                //组装ems_product_location_config配置表数据
                EmsProductLocationConfig emsProductLocationConfig = EmsProductLocationConfig.builder()
                        .productKey(productKey).moduleCode(yxVO.getModuleCode()).dit(yxVO.getDit())
                        .virtual(VirtualType.REAL.getCode()).location(Integer.parseInt(location))
                        .measureType(MeasureType.yx.getCode()).sorting(orderNum).name(nameInEms)
                        .build();
                locationConfigList.add(emsProductLocationConfig);
                String otherLocation = yxVO.getIdentificationMap().get(SceneIdentificationType.OTHER_LOCATION.getCode());
                if(otherLocation == null){
                    continue;
                }
                //其他位置信息也放到ems_product_location_config配置表中
                List<OtherLocationVO> locationList = JSON.parseArray(otherLocation, OtherLocationVO.class);
                locationList.stream().forEach(d->{
                    EmsProductLocationConfig config = EmsProductLocationConfig.builder()
                            .productKey(productKey).moduleCode(yxVO.getModuleCode()).dit(yxVO.getDit())
                            .virtual(VirtualType.REAL.getCode()).location(d.getLocation())
                            .measureType(MeasureType.yx.getCode()).sorting(d.getOrderNum()).name(d.getName())
                            .build();
                    locationConfigList.add(config);
                });
            }
        }
        //数据库删除与新增操作
        emsProductLocationConfigDAO.deleteByProductKeys(productKeys, MeasureType.yx.getCode());
        if(CollectionUtils.isNotEmpty(locationConfigList)){
            emsProductLocationConfigDAO.saveBatch(locationConfigList);
        }
    }
}
